<%-- 
    Document   : listuseraccounts
    Created on : Dec 4, 2012, 12:31:31 PM
    Author     : Quang Nhat
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>User Account List</title>
        <script type="text/javascript">
            function checkUpdate (username) {
                if (document.getElementById('txtFullname' + username).value.trim() === '') {
                    alert("Fullname cannot be empty.");
                    document.getElementById('message').innerHTML="<font color='red'>Fullname cannot be empty.</font>";
                    return false;
                }
                return true;
            }
            function filter() {
                var searchUsername = document.getElementById("SearchUsername").value;
                var searchFullname = document.getElementById("SearchFullname").value;
                var table = document.getElementById("useraccountlist");
                for(var i = 1; i < table.rows.length; i++) {
                    var row = table.rows[i];
                    var username = row.id.substring(3);
                    var fullname = document.getElementById("txtFullname" + username).value;
                    if (username.indexOf(searchUsername) >=0 && fullname.indexOf(searchFullname)>=0 ) {
                        row.style.display = "";
                    } else {
                        row.style.display = "none";
                    }
                }
            }
            function resetSearch() {
                document.getElementById("SearchUsername").value = "";
                document.getElementById("SearchFullname").value = "";
                filter();
            }
        </script>
        <script type="text/javascript" src="js/xml.js"></script>
        <script src="js/table.js" type="text/javascript"></script>
    </head>
    <body onload="filter();">
        <%@include file="/WEB-INF/jspf/Header.jspf" %>
        <h1>User Account List</h1>
        <c:set var="list" value="${requestScope.accs}"/>
        <form action="Admin" method="POST" class="add-user-form" style="margin:10px;">
            <div>
                <div>
                    <label for="AddUsername">Username <input id="AddUsername"type="text" name="txtUsername" value="" /></label>
                </div>
                <div>
                    <label for="AddPassword">Password <input id="AddPassword" type="password" name="txtPassword" value="" /></label>
                </div>
                <div>
                    <label for="AddFullname">Fullname <input id="AddFullname" type="text" name="txtFullname" value="" /></label>
                </div>
                <div class="group-control">

                    <label for="AddAdmin">Admin <input id="AddAdmin" type="checkbox" name="chkAdmin" value="ON"/></label>

                    <label for="AddAccountant">Accountant<input id="AddAccountant" type="checkbox" name="chkAccountant" value="ON"/></label>

                    <label for="AddActive">Active <input id="AddActive" type="checkbox" name="chkActive" value="ON"/></label>

                </div>
                <div class="form-control">
                    <input type="submit" value="Add" name="btnAction"/>
                </div>
            </div>
        </form>

        <div action="Admin" method="POST" class="search-user-form" style="margin:10px;">
            <div>
                <div>
                    <label >Username <input id="SearchUsername"type="text" name="txtUsername" value="" /></label>
                </div>
                <div>
                    <label >Fullname <input id="SearchFullname" type="text" name="txtFullname" value="" /></label>
                </div>
                <div class="form-control">
                    <input type="button" value="Reset" name="btnAction" onclick="resetSearch();"/>
                    <input type="button" value="Search" name="btnAction" onclick="filter();"/>
                </div>
            </div>
        </div>

        <table border="1" id="useraccountlist">
            <thead>
                <tr>
                    <th>Username</th>
                    <th>Password</th>
                    <th>Fullname</th>
                    <th>Admin</th>
                    <th>Accountant</th>
                    <th>Active</th>
                    <th>Update</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="acc" items="${list}">
                    <tr id="row${acc.username}">
                <form action="Admin" method="POST" onsubmit="return checkUpdate('${acc.username}')">

                    <td>${acc.username}
                        <input type="hidden" name="txtUsername" value="${acc.username}" />
                    </td>
                    <td><input id="txtPassword${acc.username}" type="text" name="txtPassword" value="" placeholder="Set new password here" /> </td>
                    <td><input id="txtFullname${acc.username}" type="text" name="txtFullname" value="${acc.fullname}" /> </td>
                    <td> 
                        <c:choose>
                            <c:when test="${acc.username eq sessionScope.USER.username}">
                                <input type="checkbox" name="chkAdmin" value="ON" checked="checked" disabled="disabled"/> 
                            </c:when>
                            <c:when test="${acc.admin}">
                                <input type="checkbox" name="chkAdmin" value="ON" checked="checked"/> 
                            </c:when>
                            <c:otherwise>
                                <input type="checkbox" name="chkAdmin" value="ON"/> 
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td>
                        <c:choose>
                            <c:when test="${acc.accountant}">
                                <input type="checkbox" name="chkAccountant" value="ON" checked="checked"/> 
                            </c:when>
                            <c:otherwise>
                                <input type="checkbox" name="chkAccountant" value="ON"/> 
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td>
                        <c:choose>
                            <c:when test="${acc.username eq sessionScope.USER.username}">
                                <input type="checkbox" name="chkActive" value="ON" checked="checked" disabled="disabled"/> 
                            </c:when>
                            <c:when test="${acc.active}">
                                <input type="checkbox" name="chkActive" value="ON" checked="checked"/> 
                            </c:when>
                            <c:otherwise>
                                <input type="checkbox" name="chkActive" value="ON"/> 
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td>
                        <input type="submit" value="Update" name="btnAction"/>
                    </td>
                </form>
            </tr>
        </c:forEach>
    </tbody>
</table>
</body>
</html>
